/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年11月18日         Root.Lu
 */

package com.coocaa.x.service.litepm;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;

public abstract class PackageRunnable implements Runnable {
    public interface PackageInstallerChecker {
        void onPackageInstallerAppear();

        void onPackageInstallerDisappear();
    }

    private static Context mContext = null;
    private static Thread installedChecker = null;
    private static List<PackageInstallerChecker> checkers = new ArrayList<PackageInstallerChecker>();

    private static List<String> runningPackages = new ArrayList<String>();

    public static void setContext(Context c) {
        mContext = c;
    }

    public PackageRunnable() {
        if (installedChecker == null) {
            installedChecker = new Thread("installedChecker") {
                private boolean exist() {
                    ActivityManager manager = (ActivityManager) mContext
                            .getSystemService(Context.ACTIVITY_SERVICE);

                    List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

                    if (runningTaskInfos != null) {
                        ComponentName cn = runningTaskInfos.get(0).topActivity;
                        if (cn != null) {
                            if (cn.getPackageName().contains("com.android.packageinstaller"))
                                return true;
                        }
                    }
                    return false;
                }

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    boolean prev_e = false;
                    boolean e = false;
                    while (true) {
                        int size = 0;
                        synchronized (checkers) {
                            size = checkers.size();
                        }
                        if (size > 0) {
                            e = exist();
                            if (e != prev_e) {
                                synchronized (checkers) {
                                    for (PackageInstallerChecker checker : checkers) {
                                        if (e)
                                            checker.onPackageInstallerAppear();
                                        else
                                            checker.onPackageInstallerDisappear();
                                    }
                                }
                            }
                        }
                        prev_e = e;
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException ex) {
                            // TODO Auto-generated catch block
                            ex.printStackTrace();
                        }
                    }
                }
            };
            installedChecker.start();
        }
    }

    public synchronized static void addRunningPackage(String pkgname) {
        synchronized (runningPackages) {
            if (!runningPackages.contains(pkgname))
                runningPackages.add(pkgname);
        }
    }

    public synchronized static void removeRunningPackage(String pkgname) {
        synchronized (runningPackages) {
            runningPackages.remove(pkgname);
        }
    }

    public synchronized static boolean hasRunningPackage(String pkgname) {
        synchronized (runningPackages) {
            return runningPackages.contains(pkgname);
        }
    }

    protected static void addChecker(PackageInstallerChecker checker) {
        synchronized (checkers) {
            checkers.add(checker);
        }
    }

    protected static void removeChecker(PackageInstallerChecker checker) {
        synchronized (checkers) {
            checkers.remove(checker);
        }
    }

}
